package com.example.a7822.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import org.linphone.BluetoothManager;
import org.linphone.LinphoneActivity;
import org.linphone.LinphonePreferences;
import org.linphone.assistant.RemoteProvisioningActivity;
import org.linphone.mediastream.Version;
import org.linphone.tutorials.TutorialLauncherActivity;

import static android.content.Intent.ACTION_MAIN;

/**
 * Created by flyln on 2017/10/26 22:06.
 * email:xyf_uestc@163.com
 */

/**
 *
 * Launch Linphone main activity when Service is ready.
 *
 * @author Guillaume Beraudo
 *
 */
public class YTLauncherActivity extends Activity {

    protected Handler mHandler;
    protected ServiceWaitThread mThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hack to avoid to draw twice LinphoneActivity on tablets
        if (getResources().getBoolean(org.linphone.R.bool.orientation_portrait_only)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        setContentView(org.linphone.R.layout.launch_screen);

        mHandler = new Handler();

        if (YTCallService.isReady()) {
            onServiceReady();
        } else {
            // start linphone as background
            startService(new Intent(ACTION_MAIN).setClass(this, YTCallService.class));
            mThread = new ServiceWaitThread();
            mThread.start();
        }
    }

    protected void onServiceReady() {
        final Class<? extends Activity> classToStart;
        if (getResources().getBoolean(org.linphone.R.bool.show_tutorials_instead_of_app)) {
            classToStart = TutorialLauncherActivity.class;
        } else if (getResources().getBoolean(org.linphone.R.bool.display_sms_remote_provisioning_activity) && LinphonePreferences.instance().isFirstRemoteProvisioning()) {
            classToStart = RemoteProvisioningActivity.class;
        } else {
            classToStart = LinphoneActivity.class;
        }

        // We need YTCallService to start bluetoothManager
        if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30)) {
            BluetoothManager.getInstance().initBluetooth();
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
//				startActivity(new Intent().setClass(LinphoneLauncherActivity.this, classToStart).setData(getIntent().getData()));
//				finish();
                Toast.makeText(getApplicationContext(),"服務初始化成功",Toast.LENGTH_LONG).show();
            }
        }, 1000);
    }

    private class ServiceWaitThread extends Thread {
        public void run() {
            while (!YTCallService.isReady()) {
                try {
                    sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException("waiting thread sleep() has been interrupted");
                }
            }
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    onServiceReady();
                }
            });
            mThread = null;
        }
    }
}


